package org.wsdx.Util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StringToJson {
  public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
	  test t=new test();
	 System.out.println(tranfor(t).toString().replaceAll("=", ":"));
}
  /**
   * @author Mmotiy
   * @param t
   * @return
   * @throws IllegalAccessException
   * @throws IllegalArgumentException
   * @throws InvocationTargetException
   */
  static Map<String,String> tranfor(test t) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
	  Class classT=t.getClass();
	  Method[] methods= classT.getMethods();
	  List<Method> ls=new ArrayList<>(methods.length);
	  for(Method m:methods){
		String st=  m.getName();
		if(st.matches("get.+|is.+")){
			if(!st.equals("getClass")){
				ls.add(m);
			}
		}
	  }
	  Map<String,String> map=new HashMap<>();
	  for(int i=0;i<ls.size();i++){
		  Method method=ls.get(i);
		  String name=method.getName();
		  if(name.matches("get.+")){
			  name=name.replaceFirst("get", "");
		  }else if(name.matches("is.+")){
			  name=name.replaceFirst("is", "");
		  }
		  name=name.substring(0,1).toLowerCase()+name.substring(1);
		  name="\""+name+"\"";
		  Object ob= method.invoke(t, null);
		  String obs=null;
		  if(!(ob instanceof Boolean)){
			  obs="\""+ob.toString()+"\"";
		  }else{
			  obs=String.valueOf(ob);
		  }
		  map.put(name, obs);
	  }
	  return map;
   }
}
class test{
	private String name="mmm";
	private int age=2;
	private boolean sex=true;
	public String getName() {
		return name;
	}
	public boolean isSex() {
		return sex;
	}
	public void setSex(boolean sex) {
		this.sex = sex;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}
